<<<<<<< HEAD
#include "SL.h"
void test01()
{
	SL s;
	SLInit(&s);
	SLPushBack(&s, 1);
	SLPushBack(&s, 2);
	SLPushBack(&s, 3);
	SLPrint(&s);

	SLPushFront(&s, 4);
	SLPrint(&s);

	SLPopBack(&s);
	SLPrint(&s);

	SLPopFront(&s);
	SLPrint(&s);

	SLInsert(&s, 0, 5);
	SLInsert(&s, 1, 6);
	SLPrint(&s);

	SLErase(&s, 2);
	SLPrint(&s);

}
int main()
{
	test01();


=======
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#define INIT_Capacity 4
typedef int SLDataType;
typedef struct SeqList
{
	SLDataType* arr;//数组
	int size;//有效个数
	int capacity;//数组容量
}SL;

//初始化顺序表
void SLInit(SL* ps);

//销毁
void SLDestory(SL* ps);

//打印
void SLPrint(SL* ps);

//扩容
void ExpandCapacity(SL* ps);

//尾插
void SLPushBack(SL* ps, SLDataType x);

//头插
void SLPushFront(SL* ps, SLDataType x);

//尾删
void SLPopBack(SL* ps);

//头删
void SLPopFront(SL* ps);

//插入
void SLInsert(SL* ps,int pos, SLDataType x);

//删除
void SLErase(SL* ps, int pos);

//查找
int SLFind(SL* ps, SLDataType x);


void SLInit(SL* ps)
{
	ps->arr = (SLDataType*)malloc( INIT_Capacity * sizeof(SLDataType));
	if (ps->arr == NULL)
	{
		perror("malloc");
		exit(1);

	}
	ps->size = 0;
	ps->capacity = INIT_Capacity;
}

void SLDestory(SL* ps)
{
	free(ps->arr);
	ps->arr = NULL;
	ps->size = ps->capacity = 0;
}

void SLPushBack(SL* ps, SLDataType x)
{
	assert(ps);
	//当数组容量满了时
	if (ps->size == ps->capacity)
	{
		//扩容
		ExpandCapacity(ps);
	}

			
	ps->arr[ps->size] = x;
	ps->size++;

	//SLInsert(ps,ps->size - 1,  x);
}


void SLPrint(SL* ps)
{

	int i = 0;
	for (i = 0; i < ps->size; i++)
	{
		printf("%d ", ps->arr[i]);
	}

	printf("\n");

}
//扩容
void ExpandCapacity(SL* ps)
{
	int newCapacity = 2 * ps->capacity;
	SLDataType* temp = (SLDataType*)realloc(ps->arr, newCapacity * sizeof(SLDataType));

	if (!temp)
	{
		printf("内存分配失败!\n");
		exit(1);
	}

	ps->arr = temp;
	ps->capacity = newCapacity;
	printf("扩容成功：%d→%d\n", ps->capacity / 2, newCapacity);
}


void SLPushFront(SL* ps, SLDataType x)
{
	assert(ps);
	assert(ps->size > 0);
	if (ps->size == ps->capacity)
	{
		//扩容
		ExpandCapacity(ps);
	}

	int i = 0;
	for (i = (ps->size) - 1; i >= 0; i--)
	{
		ps->arr[i + 1] = ps->arr[i];
	}

	ps->arr[0] = x;
	ps->size++;

	//SLInsert( ps,0,  x);
}

void SLPopBack(SL* ps)
{
	assert(ps);
	//判断为空和删空情况
	assert(ps->size > 0);

	ps->size--;


	//SLErase(ps, ps->size);
}
void SLPopFront(SL* ps)
{
	assert(ps);

	int i = 0;
	for (i = 0; i < ps->size; i++)
	{
		ps->arr[i] = ps->arr[i + 1];

	}
	ps->size--;
//SLErase(ps, 0);
}



void SLInsert(SL* ps, int pos, SLDataType x)
{
	assert(ps);
	assert(pos >= 0 && pos <= ps->size);

	if (ps->size == ps->capacity)
	{
		//扩容
		ExpandCapacity(ps);
	}

	int end = ps->size - 1;
	while (end >= pos)
	{

		ps->arr[end + 1] = ps->arr[end];
		end--;
	}

	ps->arr[pos] = x;
	ps->size++;
}

void SLErase(SL* ps, int pos)
{
	assert(ps);
	assert(pos >= 0 && pos <= ps->size);
	int begin = pos + 1;
	while(begin < ps->size)
	{
		//让pos后面的数据覆盖他
		ps->arr[begin-1] = ps->arr[begin];
		begin++;
	}

	ps->size--;
}


int SLFind(SL* ps, SLDataType x)
{
	assert(ps);

	for (int i = 0; i < ps->size; i++)
	{
		if (ps->arr[i] == x)
			return x;
	}


	return -1;
>>>>>>> efa49190f7bd011a3e1ff9a0b6f1db6410d17742
}










<<<<<<< HEAD

=======
>>>>>>> efa49190f7bd011a3e1ff9a0b6f1db6410d17742
